﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GameBoardEditor : MonoBehaviour {

	private GameBoard board;
	private GameField curField; //data definition
	
	void Awake() {
		board = GetComponent<GameBoard>();
		rectMatch = new Rect(Screen.width - 100, 0, 100, 50);
		rectPool = new Rect(Screen.width - 100, 50, 100, 50);
	}
	
	// Use this for initialization
	void Start () {
		// load existing fields (layouts)
		GameFieldSet playerFields;
		GameSaveLoad.LoadFields(out playerFields);
		GameFieldSet.playerFields = playerFields;
		curField = GameFieldSet.activeField;
		if (curField.size == 0)
			curField.size = board.boardSize;
		
		Refresh();
	}
	
	// Update is called once per frame
	void Update () {
		Vector2 gamePos;
		if (board.GetTouchInput( out gamePos )) {
			int cellIndex = board.GetCellIndex( gamePos );
			if (curField.lockedCells.Contains(cellIndex)) {
				curField.lockedCells.Remove(cellIndex);
				board.SetCellLocked( gamePos, false );
			}
			else {
				curField.lockedCells.Add(cellIndex);
				board.SetCellLocked( gamePos, true );
			}
		}
	}
	
	void Refresh() {
		// Load visual board
		board.boardSize = curField.size;
		board.Load();
		foreach (int cellIndex in curField.lockedCells) {
			board.SetCellLocked(cellIndex, true);
		}
		
		GameFieldSet fieldSet = GameFieldSet.playerFields;
		strNext = string.Format("{0}/{1}", fieldSet.fieldIndex + 1, fieldSet.fields.Count);
	}

	//gui options
	private Rect rectReset = new Rect(0, 0, 100, 50);
	private Rect rectNew = new Rect(0, 50, 100, 50);
	private Rect rectDelete = new Rect(0, 100, 100, 50);
	private Rect rectSave = new Rect(0, 150, 100, 50);
	private Rect rectNext = new Rect(0, 200, 100, 50);

	private Rect rectMatch;
	private Rect rectPool;
	
	private string strNext = "X/X"; //current/total
	
	void OnGUI() {
		if (GUI.Button(rectReset, "RESET")) {
			curField.lockedCells.Clear();
			board.Load();
		}
		
		if (GUI.Button(rectNew, "NEW")) {
			GameFieldSet fieldSet = GameFieldSet.playerFields;
			
			curField = new GameField();
			curField.size = board.boardSize;
			fieldSet.fields.Add(curField);
			fieldSet.fieldIndex = GameFieldSet.playerFields.fields.Count - 1;
			
			GameFieldSet.activeField = curField;
			Refresh();
		}
		
		if (GUI.Button(rectDelete, "DELETE")) {
			//TODO- GameFieldSet.DeleteCurrent();
			GameFieldSet fieldSet = GameFieldSet.playerFields;
			if (fieldSet.fields.Count > 1) {
				fieldSet.fields.RemoveAt(fieldSet.fieldIndex);
				if (fieldSet.fieldIndex >= fieldSet.fields.Count) //removed last
					fieldSet.fieldIndex--;
					
				curField = fieldSet.fields[fieldSet.fieldIndex];
				GameFieldSet.activeField = curField;
				Refresh();
			}
		}
		
		if (GUI.Button(rectNext, strNext)) { //NEXT
			//TODO- GameFieldSet.SelectNext();
			GameFieldSet fields = GameFieldSet.playerFields;
			fields.fieldIndex++;
			if (fields.fieldIndex >= fields.fields.Count)
				fields.fieldIndex = 0;
			
			curField = fields.fields[fields.fieldIndex];
			curField.size = board.boardSize;
			GameFieldSet.activeField = curField;
			Refresh();
		}
		
		if (GUI.Button(rectSave, "SAVE")) {
			GameSaveLoad.SaveFields( GameFieldSet.playerFields );
		}
		
		if (GUI.Button(rectMatch, "MATCH")) {
			Application.LoadLevel("Match"); //battle
		}
		
		if (GUI.Button(rectPool, "POOL")) {
			Application.LoadLevel("Pool"); //pool manager
		}
	}
}
